package com.guhappy.util;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.apache.log4j.Logger;
import org.hibernate.SessionFactory;
import org.hibernate.StaleObjectStateException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.guhappy.util.HibernateUtil;

public class HibernateSessionRequestFilter implements Filter {
 
    private static final Logger logger = Logger.getLogger(HibernateSessionRequestFilter.class);
 
    /**
	 * @uml.property  name="sf"
	 * @uml.associationEnd  
	 */
    private SessionFactory sf;
 
    public void doFilter(ServletRequest request,
                         ServletResponse response,
                         FilterChain chain)
            throws IOException, ServletException {
    	
    	HttpServletRequest req = (HttpServletRequest) request;
    	@SuppressWarnings("unused")
		HttpServletResponse resp = (HttpServletResponse) response;
		if (req.getServletPath().indexOf(".action") != -1 && req.getServletPath().indexOf("json") == -1)
		{
	        try {
	        	//logger.debug("Starting a database transaction");
	            sf.getCurrentSession().beginTransaction();
	 
	            // Call the next filter (continue request processing)
	            chain.doFilter(request, response);
	 
	            // Commit and cleanup
	            //logger.debug("Committing the database transaction");
	            sf.getCurrentSession().getTransaction().commit();
	 
	        } catch (StaleObjectStateException staleEx) {
	            logger.error("This interceptor does not implement optimistic concurrency control!");
	            logger.error("Your application will not work until you add compensation actions!");
	            // Rollback, close everything, possibly compensate for any permanent changes
	            // during the conversation, and finally restart business conversation. Maybe
	            // give the user of the application a chance to merge some of his work with
	            // fresh data... what you do here depends on your applications design.
	            throw staleEx;
	        } catch (Throwable ex) {
	            // Rollback only
	            ex.printStackTrace();
	            try {
	                if (sf.getCurrentSession().getTransaction().isActive()) {
	                    logger.debug("Trying to rollback database transaction after exception");
	                    sf.getCurrentSession().getTransaction().rollback();
	                }
	            } catch (Throwable rbEx) {
	                logger.error("Could not rollback transaction after exception!", rbEx);
	            }
	 
	            // Let others handle it... maybe another interceptor for exceptions?
	            throw new ServletException(ex);
	        }
	    }
        else 
        {
			logger.info("ignoring " + req.getServletPath());
			chain.doFilter(request, response);
			return;
		}
    }
 
    public void init(FilterConfig filterConfig) throws ServletException {
        logger.debug("Initializing filter...");
        logger.debug("Obtaining SessionFactory from static HibernateUtil singleton");
        sf = HibernateUtil.getSessionFactory();
    }
 
    public void destroy() {}

 
}
